#include "chat_room.h"

linklist* Createlinklist(){
    linklist* h=(linklist*)malloc(sizeof(linklist));
    if(h==NULL){
        printf("malloc error,file:%s,line%d\n",__FILE__,__LINE__);
        return NULL;
    }
    h->next=NULL;
    return h;
}

void udpChatLogin(const int sockfd,MSG msg,linklist* h,struct sockaddr_in clientaddr){
    linklist *p = h;
    while(p->next!=NULL){
        p=p->next;
        if(strcmp(p->name,msg.name)==0){
            msg.code=-1;
            strcpy(msg.text,"User name exists");
            if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr))==-1){
                ERRLOG("sendto error");
            }
            return ;
        }
    }

    strcpy(msg.text,"Successfully logged in");
    if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr))==-1){
        ERRLOG("sendto error");
    }
    printf("%s 上线\n",msg.name);
    
    p=h;
    char buf[N]={0};
    sprintf(buf,"-------------------------------------%s 上线了--------------------------------\n",msg.name);
    strcpy(msg.text,buf);
    while(p->next!=NULL){
        p=p->next;
        if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&(p->addr),sizeof(p->addr))==-1){
            ERRLOG("sendto error");
        }
    }

    linklist* pnew=(linklist*)malloc(sizeof(linklist));
    if(pnew==NULL){
        printf("malloc error,file:%s,line%d\n",__FILE__,__LINE__);
        return ;
    }

    strcpy(pnew->name,msg.name);
    pnew->addr=clientaddr;
    pnew->next=NULL;

    pnew->next=h->next;
    h->next=pnew;
}

void udpBroadCast(const int sockfd,MSG msg,linklist* h,struct sockaddr_in clientaddr){
    char buf[M]={0};
    printf("%s:%s\n",msg.name,msg.text);
    sprintf(buf,"%s:%s",msg.name,msg.text);
    strcpy(msg.text,buf);

    linklist *p = h;
    while(p->next!=NULL){
        p=p->next;
        if(memcmp(&p->addr,&clientaddr,sizeof(clientaddr))==0){
            continue;
        }
        if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->addr,sizeof(p->addr))==-1){
            ERRLOG("sendto error");
        }
    }
}

void udpChatQuit(const int sockfd,MSG msg,linklist* h,struct sockaddr_in clientaddr){
    char buf[N]={0};
    printf("%s 下线\n",msg.name);
    sprintf(buf,"-------------------------------%s 下线了----------------------------------",msg.name);
    strcpy(msg.text,buf);

    linklist *p=h;
    linklist *del=NULL;
    while(p->next!=NULL){
        if(memcmp(&p->next->addr,&clientaddr,sizeof(clientaddr))==0){
            del=p->next;
            p->next=del->next;
            free(del);
            del=NULL;
        }else{
            p=p->next;
            if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->addr,sizeof(p->addr))==-1){
                ERRLOG("sendto error");
            }
        }
    }
}

void udpChatPrivate(const int sockfd,MSG msg,linklist* h){
    char name[N]={0};
    char text[N]={0};

    bool flag=false;
    int i=strlen("private to ");
    int j=0;

    for(;msg.text[i]!=' ';i++,j++){
        name[j]=msg.text[i];
    }

    j=0;
    while(msg.text[i]!='\0'){
        text[j]=msg.text[i];
        i++;
        j++;
    }
    strcpy(msg.text,text);

    linklist* p= h;

    while(p->next!=NULL){
        p=p->next;
        if(strcmp(p->name,name)==0){
            if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->addr,sizeof(p->addr))==-1){
                ERRLOG("sendto errot");
            }
            flag=true;
        }
    }

    if(!flag){
        if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->addr,sizeof(p->addr))==-1){
            ERRLOG("sendto error");
        } 
    }
}

